//链接地址获取参数
var id = globalFun.GetQueryString('id');
var fromApp=globalFun.GetQueryString("from");
//var consultationId = 2162;
//var uuid = '5895e62d36874626828b947e707a0484';
//var phone = 13717847870;


//屏幕下滑 zx_head0出现
$("#waimian").scroll(function(){
	// tips(1111)
 //    var htmlFontsize=document.documentElement.style.fontSize;
 //    htmlFontsize=htmlFontsize.slice(0,htmlFontsize.length-2);
	var limitTop=$(this).scrollTop();
	if(limitTop < 70){
		$(".zx_head0").hide();
	}else{
		$(".zx_head0").show();
	}
});


var ua = navigator.userAgent.toLowerCase();
var scStatus;//收藏状态


//刷新评论列表
if (/pjyc_i/.test(ua)) {
//IOS通知刷新评论列表
            setupWebViewJavascriptBridge(function(bridge){
                    bridge.registerHandler("flush",function(data,responseCallback){
                                details();
              });
           });
      } else if (/pujiyicheng/.test(ua)) {
//从android通知刷新评论列表
        function  flush(){
                  details();
        }
      }
//刷新回复列表
if (/pjyc_i/.test(ua)) {
//IOS通知刷新评论列表
            setupWebViewJavascriptBridge(function(bridge){
                    bridge.registerHandler("flush_hf",function(data,responseCallback){
                                details();
              });
           });
      } else if (/pujiyicheng/.test(ua)) {
//从android通知刷新评论列表
        function  flush_hf(){
                  details();
        }


      }
//评论
$("#com_input").click(function(){
   if (/pjyc_i/.test(ua)) {
    setupWebViewJavascriptBridge(function(bridge){
            bridge.callHandler("jsToClientComment",id,function(data,responseCallback){
    });
            });
    } else if (/pujiyicheng/.test(ua)) {
            window.stub.jsToClientComment(id);
    }
});
//回复
$(document).on('click','.huifu',function(){
   var plid = $(this).next().attr('data-id');
   console.log(plid)
   if (/pjyc_i/.test(ua)) {
    setupWebViewJavascriptBridge(function(bridge){
            bridge.callHandler("jsToClientCommentHf",plid,function(data,responseCallback){
    });
            });
    } else if (/pujiyicheng/.test(ua)) {
            window.stub.jsToClientCommentHf(plid);
    }
});
//返回
$("#go_back").click(function(){
	if(!fromApp){
		window.history.back(-1);
	}else{
	if (mark===1) {
	  setupWebViewJavascriptBridge(function(bridge){
	 	 	bridge.callHandler('goBack',function(resp){});
	});
		} else if (mark===0) {
	         //var result = lase;
	         window.stub.goBack();//用接口stub, 通过调用内部类中的方法jsMethod给javas传回result。

	    }
	}
});
//头像
$(document).on('click','.first_pic',function(){
   var txid = $(this).parent().attr('pl-id');
   var issf = $(this).parent().attr('issf');
   console.log(txid);
   console.log(issf);

   if (issf == 1){
   	window.location.href =rootdomain+ 'html/service/serviceList.html?id=' + txid
   }
   // if (/pjyc_i/.test(ua)) {
   //  setupWebViewJavascriptBridge(function(bridge){
   //          bridge.callHandler("jsToClientUserDetail",txid,function(data,responseCallback){
   //  });
   //          });
   //  } else if (/pujiyicheng/.test(ua)) {
   //          window.stub.jsToClientUserDetail(txid);
   //  }
});

//头像
$(document).on('click','.limian',function(){
   var txid = $(this).attr('pl-id');
   var issf = $(this).attr('issf');
   console.log(txid);
   console.log(issf);
   if (issf == 1){
   	window.location.href =rootdomain+ 'html/service/serviceList.html?id=' + txid
   }
   // if (/pjyc_i/.test(ua)) {
   //  setupWebViewJavascriptBridge(function(bridge){
   //          bridge.callHandler("jsToClientUserDetail",txid,function(data,responseCallback){
   //  });
   //          });
   //  } else if (/pujiyicheng/.test(ua)) {
   //          window.stub.jsToClientUserDetail(txid);
   //  }
});


//展开剩余回复评论
function huifu(dataObj){
    	var ua = navigator.userAgent.toLowerCase();
        if (/pjyc_i/.test(ua)) {
            setupWebViewJavascriptBridge(function(bridge){
                bridge.callHandler('jsSurpluscomment',dataObj,function(resp){});
              });
            } else if (/pujiyicheng/.test(ua)) {
                //var result = lase;
               window.stub.jsSurpluscomment(dataObj);//用接口stub, 通过调用内部类中的方法jsMethod给javas传回result。

         }
	}
	$(document).on('click','.check_nore',function(){
		var plid = $(this).attr('pl-id');
		console.log(plid)
		if(Auth()){
			var dataObj = {
				id:id,
				plid:plid
			}
			var dataObjT = JSON.stringify(dataObj);
			huifu(dataObjT);
		} else{
			sessionStorage.setItem('history', window.location.href);
			toClientLogin(location.href);
		}
	})



//咨询详情 渲染页面

var title;
var content;
var bid;

function details(){

var dataObj = {
		consultationId:id,
   		memberId:''
}
if(Auth()){
	dataObj.memberId = user_info.id
}
$.ajax({
	type:"post",
   	url:apicom+"mobile/view/consultation/getConsultationMessage",
// 	url:"http://192.168.30.225:10001/mobile/view/consultation/getConsultationMessage",
 	data:dataObj,
 	async: false,
	dataType: "json",
	success:function(res){
		if(typeof(res) == 'string'){
			res = JSON.parse(res);
		}
		 console.log(res);
		 if(res.code == 1){
		 	var aa = '';
		 	var bb = '';
		 	$('.zx_title').html(res.title);
		 	$('.zx_center').html(res.description);
		 	$('.plzs').html(res.bCount);
		 	$('.tjts').html(res.aCount);

		 	title = res.title;
		 	content =res.textDescription;
		 	bid = res.memberId;

		 	//服务


		 	if(res.serviceProjectList == '' || res.serviceProjectList == null){
		 		$('.tj_service').hide()
		 	} else{
		 		$('.tj_service').show();

		 		if(res.serviceProjectList.length <= 3){

			 		$('.tj_loadmore').hide();
			 	} else{
			 		$('.tj_loadmore').show();

			 	}

			 	for(var i = 0; i < res.serviceProjectList.length; i++){
			 		if(i<3){
			 			var classname="waimian"
			 		}else{
			 			var classname="waimian x_hide"
			 		}

			 		aa += '<div class="'+classname+'"><p class="tj_title">'+res.serviceProjectList[i].reasons+'</p><div class="tj_team" service-id='+res.serviceProjectList[i].id+'><span class="tj_pic"><img src="'+res.serviceProjectList[i].imageAddress+'" alt="" /></span><p class="tj_order">'+res.serviceProjectList[i].title+'</p>'
			 		if(res.serviceProjectList[i].introduce == null){
			 			aa += '<p class="tj_price">¥<b class="jq">'+res.serviceProjectList[i].price+'</b><span>去购买</span></p></div></div>'
			 		} else{
			 			aa += '<p class="tj_descript">'+res.serviceProjectList[i].introduce+'</p><p class="tj_price">¥<b class="jq">'+res.serviceProjectList[i].price+'</b><span>去购买</span></p></div></div>'
			 		}


		 	}
		 		$('.tj_content').html(aa);
		 		$('.x_hide').hide();
		 	}


		 	//评论
		 	for(var j = 0; j < res.commentLists.length; j++){

		 		bb += '<div class="comment_list"><div class="comment_first" pl-id='+res.commentLists[j].advisorId+' issf='+res.commentLists[j].isMerchant+'><span class="first_pic">'
		 		if(res.commentLists[j].logoAddress == null){
		 			bb += '<img src="../../img/b_moren3x.png" alt="" />'
		 		} else{
		 			bb += '<img src="'+res.commentLists[j].logoAddress+'" alt="" />'
		 		}

		 		bb += '</span><p class="first_title">'+res.commentLists[j].nickName+'</p><p class="first_content">'+res.commentLists[j].content+'</p><p class="first_time">'+res.commentLists[j].createTime+'<span class="dianzan1"><b class="huifu">回复</b><i  data-id="'+res.commentLists[j].commentId+'" class="dianZan">'

		 		if(res.commentLists[j].consultationCommentFabulousStatus == 0){
					bb+='<img data-type="1" src="../../img/a_zan.png"/>'
				}else if(res.commentLists[j].consultationCommentFabulousStatus == 2){
					bb+='<img data-type="1" src="../../img/a_zan.png"/>'
				} else if(res.commentLists[j].consultationCommentFabulousStatus == 1){
					bb+='<img data-type="2" src="../../img/a_zan_1.png"/>'
				} else{
					bb+='<img data-type="1" src="../../img/a_zan.png"/>'
				}

		 		bb += '</i>'

		 		if(res.commentLists[j].fabulousNum == null){
		 			bb += '<b class="first_num">0</b>'
		 		} else {
		 			bb += '<b class="first_num">'+res.commentLists[j].fabulousNum+'</b>'
		 		}
		 		bb += '</span></p></div><div class="comment_second">'

		 		var pl = res.commentLists[j].replyList;
		 		var plcd = res.commentLists[j].replyList.length;

		 				console.log(plcd)



		 			for(var k = 0; k < pl.length; k++){

		 				if(k<3){
				 			var classname="waimian"
				 		}else{
				 			var classname="waimian x_hide"
				 		}




		 				bb += '<div class="'+classname+' second_list"><p class="second_pic"><span class="limian" pl-id='+pl[k].advisorId+' issf='+pl[k].isMerchant+'>'
		 				if(pl[k].logoAddress == null){
				 			bb += '<img src="../../img/b_moren3x.png" alt="" />'
				 		} else{
				 			bb += '<img src="'+pl[k].logoAddress+'" alt="" />'
				 		}

		 				bb += '</span>'+pl[k].nickName+'</p><p class="second_content">'+pl[k].content+'</p><p class="second_time">'+pl[k].createTime+'<span><i data-id="'+pl[k].commentId+'" class="dianZan2">'

		 			if(pl[k].consultationCommentFabulousStatus == 0){
						bb+='<img data-type="1" src="../../img/a_zan.png"/>'
					}else if(pl[k].consultationCommentFabulousStatus == 2){
						bb+='<img data-type="1" src="../../img/a_zan.png"/>'
					} else if(pl[k].consultationCommentFabulousStatus == 1){
						bb+='<img data-type="2" src="../../img/a_zan_1.png"/>'
					} else{
						bb+='<img data-type="1" src="../../img/a_zan.png"/>'
					}

		 			bb += '</i>'

		 			if(pl[k].fabulousNum == null){
		 				bb += '<b class="first_num">0</b>'
		 			} else{
		 				bb += '<b class="first_num">'+pl[k].fabulousNum+'</b>'
		 			}
		 			bb += '</span></p></div>'
		 			}

		 			if(pl.length > 3){
		 				bb += '<p class="check_nore" pl-id='+res.commentLists[j].commentId+'>查看'+plcd+'条回复</p>'
		 			}
		 			bb += '</div></div>'


		 	}
		 		$('.comment').html(bb);
		 		$('.x_hide').hide();


		 	//关注
			if(res.consultationConcernStatus == 2){
				$('.attention').html('关注')
				$('.attention').attr('type','0')
			}else if(res.consultationConcernStatus == 0){
				$('.attention').html('关注')
				$('.attention').attr('type','0')
			} else if(res.consultationConcernStatus == 1){
				$('.attention').html('已关注')
				$('.attention').attr('type','1')
			} else{
				$('.attention').html('关注')
				$('.attention').attr('type','0')
			}
			//收藏
			if(res.consultationCollectStatus == 1){
				$('.handle_star').attr('type',"5")
 				$('.navDp-imga').attr('src','../../img/a_shoucang.png');
 			} else{
 				$('.handle_star').attr('type',"2")
				$('.navDp-imga').attr('src','../../img/a_sc.png');
 			}
 			//点赞
			if(res.consultationFabulousStatus == 1){
				$('.handel_praise').attr('type',"2")
 				$('.dianzan').attr('src','../../img/a_zan_1.png');
 			} else{
 				$('.handel_praise').attr('type',"1")
				$('.dianzan').attr('src','../../img/a_zan.png');
 			}

		 } else{
		 	tips(res.msg)
		 }
	},
	error:function(err){
   		console.log(err);
	}
})
}

details()

//点击展开
$('.tj_loadmore').on('click',function(){

	if($('.tj_loadmore').html() == '收起'){
		$('.tj_loadmore').html('展示');

		$('.x_hide').hide();
	} else{
		$('.tj_loadmore').html('收起');
		$('.x_hide').show();
	}
})




/* 关注与取消关注 */
function attention(id,type){
	if(Auth()){
		var dataObj = {
			bid:bid,
			type:type,
			uuid:user_info.uuid,
 			phone:user_info.phone,
		}
	}else{
		toClientLogin(location.href);
		return false;
	}
	$.ajax({
		url: apicom + "mobile/after/memberfollow/attention",
//		url: "http://192.168.30.225:10001/mobile/after/memberfollow/attention",
		type: 'POST',
		dataType: 'json',
		data: dataObj,
		success:function(result){
			var res;
			if(typeof(result) == "string"){
				res = JSON.parse(result);
			}else{
				res = result
			}
			if(res.code == 1){
				//提示
				layer.open({
				    content: res.msg
				    ,skin: 'msg'
				    ,time: 2 //2秒后自动关闭
				});
				if(type==0){
					$('.zx_gz').html('已关注')
					$('.zx_gz').attr('type','1');
				}else if(type==1){
					$('.zx_gz').html('关注');
					$('.zx_gz').attr('type','0');
				}
				$('.zx_gz').removeClass('attention');
				setTimeout(function(){
					$('.zx_gz').addClass('attention');
				},2000)
			}else{
				tips(res.msg);
			};
		},
		 error:function(err){
                tips("服务器错误！");
         }
	})
}


	$(document).on('click','.attention',function(){
		var type=$(this).attr('type');
		if(type==0){
			attention(id,0)
		}else{
			attention(id,1)
		}
	})

//收藏与取消收藏
	$('.handle_star').on('click',function(){
		if(Auth()){
			var status = $(this).attr('type');
				// console.log(dataObj)
				var dataObj = {
					consultationId:id,
					uuid:user_info.uuid,
 					phone:user_info.phone,
					type:status,
					memberId:user_info.id
				}

				$.ajax({
					type:"post",
				 	url:apicom + "mobile/after/memberfollow/collection ",
//				 	url:"http://192.168.30.225:10001/mobile/after/memberfollow/collection ",
				 	data:dataObj,
				 	async: false,
					dataType: "json",
					success:function(res){
						if(typeof(res) == 'string'){
							res = JSON.parse(res);
						}
						console.log(res);
						if(res.code == 1){
							if(status == 2){
								$('.handle_star').attr('type','5');
								$('.navDp-imga').attr('src','../../img/a_shoucang.png');
							} else{
								$('.handle_star').attr('type','2');
								$('.navDp-imga').attr('src','../../img/a_sc.png');
							}
						}
						tips(res.msg)
					},
					error:function(err){
						console.log(err)
					}
				})
		} else{
			sessionStorage.setItem('history', window.location.href);
			toClientLogin(location.href)
		}
	})

//点赞与取消点赞
$('.handel_praise').on('click',function(){
	if(Auth()){
		var status = $(this).attr('type');
			// console.log(dataObj)
			var dataObj = {
				consultationId:id,
				consultationType:0,
				uuid:user_info.uuid,
					phone:user_info.phone,
				type:status,
				memberId:user_info.id
			}

			$.ajax({
				type:"post",
				 	url:apicom + "mobile/after/consulationfabulous/fabulous",
	//			 	url:"http://192.168.30.225:10001/mobile/after/consulationfabulous/fabulous ",
			 	data:dataObj,
			 	async: false,
				dataType: "json",
				success:function(res){
					if(typeof(res) == 'string'){
						res = JSON.parse(res);
					}

					if(res.code == 1){
						if(status == 1){
							$('.handel_praise').attr('type','2');
							$('.dianzan').attr('src','../../img/a_zan_1.png');
						} else{
							$('.handel_praise').attr('type','1');
							$('.dianzan').attr('src','../../img/a_zan.png');
						}
					}
					tips(res.msg)
				},
				error:function(err){
					console.log(err)
				}
			})
		} else{
			sessionStorage.setItem('history', window.location.href);
			toClientLogin(location.href)
		}
	})

//评论一级点赞
$(document).on('click','.dianZan',function(){
	var dianzan1 = $(this);
	var pid = $(this).attr('data-id');
	var zt = $(this).children("img").attr("data-type");
	if(!Auth()){
		toClientLogin();
	}else{
	 	if(zt == 1){
	 		$.ajax({
	 			type:"post",
	 			url:apicom+"mobile/after/consulationfabulous/fabulous",
//			 	url:"http://192.168.30.225:10001/mobile/after/consulationfabulous/fabulous ",

	 			data:{
	 				consultationId:id,
	 				type:1,
	 				consultationType:2,
	 				consultationCommentId:pid,
	 				productId:'',
	 				orderCommentId: '',
	 				phone:user_info.phone,
	 				uuid:user_info.uuid
	 			},
	 			success:function(res){
			        if(typeof res == "string"){
			            var data=JSON.parse(res);
			        }else{
			            var data=res;
			        }
			        if(data.code == 1){
			        	tips(data.msg);
			        	dianzan1.addClass('red');
			        	dianzan1.find('img').attr('src','../../img/a_zan_1.png');
			        	dianzan1.children("img").attr("data-type",2);
			        	var str1 = dianzan1.next().html();
			        	dianzan1.next().html( str1*1 + 1);
			        }
	 			},
	 			error:function(err){
	 				tips("服务器错误!");
	 			}
	 		});
	 	}else{
	 		$.ajax({
	 			type:"post",
	 			url:apicom+"mobile/after/consulationfabulous/fabulous",
//			 	url:"http://192.168.30.225:10001/mobile/after/consulationfabulous/fabulous ",

	 			data:{
	 				consultationId:id,
	 				type:2,
	 				consultationType:2,
	 				consultationCommentId:pid,
	 				productId:'',
	 				orderCommentId: '',
	 				phone:user_info.phone,
	 				uuid:user_info.uuid
	 			},
	 			success:function(res){
			        if(typeof res == "string"){
			            var data=JSON.parse(res);
			        }else{
			            var data=res;
			        }
			        if(data.code == 1){
			        	tips("取消点赞");
			        	dianzan1.removeClass('red');
			        	dianzan1.find('img').attr('src','../../img/a_zan.png');
			        	dianzan1.children("img").attr("data-type",1);
			        	var str1 = dianzan1.next().html();
			        	dianzan1.next().html( str1*1 -1 );

			        }
	 			},
	 			error:function(err){
	 				tips("服务器错误!");
	 			}
	 		});
	 	}
	}

 });

 //二级回复点赞
 $(document).on('click','.dianZan2',function(){
	var dianzan1 = $(this);
	var pid = $(this).attr('data-id');
	var zt = $(this).children("img").attr("data-type");
	if(!Auth()){
		toClientLogin();
	}else{
	 	if(zt == 1){
	 		$.ajax({
	 			type:"post",
	 			url:apicom+"mobile/after/consulationfabulous/fabulous",
//			 	url:"http://192.168.30.225:10001/mobile/after/consulationfabulous/fabulous ",

	 			data:{
	 				consultationId:id,
	 				type:1,
	 				consultationType:2,
	 				consultationCommentId:pid,
	 				productId:'',
	 				orderCommentId: '',
	 				phone:user_info.phone,
	 				uuid:user_info.uuid
	 			},
	 			success:function(res){
			        if(typeof res == "string"){
			            var data=JSON.parse(res);
			        }else{
			            var data=res;
			        }
			        if(data.code == 1){
			        	tips(data.msg);
			        	dianzan1.addClass('red');
			        	dianzan1.find('img').attr('src','../../img/a_zan_1.png');
			        	dianzan1.children("img").attr("data-type",2);
			        	var str1 = dianzan1.next().html();
			        	dianzan1.next().html( str1*1 + 1);
			        }
	 			},
	 			error:function(err){
	 				tips("服务器错误!");
	 			}
	 		});
	 	}else{
	 		$.ajax({
	 			type:"post",
	 			url:apicom+"mobile/after/consulationfabulous/fabulous",
//			 	url:"http://192.168.30.225:10001/mobile/after/consulationfabulous/fabulous ",

	 			data:{
	 				consultationId:id,
	 				type:2,
	 				consultationType:2,
	 				consultationCommentId:pid,
	 				productId:'',
	 				orderCommentId: '',
	 				phone:user_info.phone,
	 				uuid:user_info.uuid
	 			},
	 			success:function(res){
			        if(typeof res == "string"){
			            var data=JSON.parse(res);
			        }else{
			            var data=res;
			        }
			        if(data.code == 1){
			        	tips("取消点赞");
			        	dianzan1.removeClass('red');
			        	dianzan1.find('img').attr('src','../../img/a_zan.png');
			        	dianzan1.children("img").attr("data-type",1);
			        	var str1 = dianzan1.next().html();
			        	dianzan1.next().html( str1*1 -1 );

			        }
	 			},
	 			error:function(err){
	 				tips("服务器错误!");
	 			}
	 		});
	 	}
	}

 });

//服务项目跳转到服务项目详情
$(document).on('click','.tj_team',function(){
		sessionStorage.setItem("fromHTML","H5");
		var id = $(this).attr('service-id');
		window.location.href = '../homektxq/home_fwxq.html?id=' + id
})


//分享
function zhuanfa(data) {
  if (/pjyc_i/.test(ua)) {

    setupWebViewJavascriptBridge(function(bridge) {
      bridge.callHandler('jsToClientShare', data, function(resp) {});
    });
  } else if (/pujiyicheng/.test(ua)) {
    //var result = lase;
    var data2 = JSON.stringify(data);

    window.stub.jsToClientShare(data2); //用接口stub, 通过调用内部类中的方法jsMethod给java传回result。

  }
}

$('.zx_share').on('click', function() {
    	if(Auth()){
    		var data = {
    			uuid:user_info.uuid,
    			phone:user_info.phone,
    			memberId:user_info.id
    		}
    		// shareCallBack(id)

    	} else{
    		sessionStorage.setItem('history', window.location.href);
    		toClientLogin(location.href)
    	}

    		var $self = $(this)
    		if($self.hasClass("disabled")){
    			return false
    		}
    		$self.addClass("disabled")
    		setTimeout(function(){
    			$self.removeClass("disabled")
    		},1000)

	  var data = {
	    type: 1,
	    url:location.href + '&share=' + user_info.id,
	    title: title,
	    content: content
	  }
	  zhuanfa(data);
	})
// -----------------------------------------------------------------------------------------------------------



	/* 分享回调 */
	function shareCallBack(data1){
		var dataObj={
			type:0,
			id:data1,
			phone:user_info.phone,
			uuid:user_info.uuid
		}
		$.ajax({
				url: apicom + "mobile/after/share/share",
				type: 'POST',
				dataType: 'json',
				data: dataObj,
				success:function(result){
					var res;
					if(typeof(result) == "string"){
						res = JSON.parse(result);
					}else{
						res = result
					}
					if(res.code == 1){
						// tips(22222);
						//需要进行的操作
					}
				},
				error:function(){
					//提示
					layer.open({
					    content: '服务器连接失败'
					    ,skin: 'msg'
					    ,time: 2 //2秒后自动关闭
					});
				}
			})
	}


		 if (mark==1) {
         setupWebViewJavascriptBridge(function(bridge){
           bridge.registerHandler('clientToJsShare',function(data,resp){
           			// tips(3333333)
           			shareCallBack(id)
           })
         })
       } else if(mark==0) {
       		function clientToJsShare(){
       				// tips(1111111);
           			shareCallBack(id)
       		}
       }
